package com.ews.onlineexchange.model;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import com.ews.onlineexchange.enumType.RecordStatus;

/**
 * 审核通过后生成的记录
 * 
 * @author Administrator
 *
 */
@Entity
public class DeterminedRecord extends DataEntity {

	/**
	 * 1.当deleted为true,currentSatus为true,表示整改完成（不能修改）即已被移除黑名单
	 * 2.当deleted为false,currentSatus为true,表示删除状态审核未通过
	 * 3.当deleted为true,currentSatus为false,表示保留状态审核通过
	 * 4.当deleted为false,currentSatus为false,表示保留状态审核未通过 5.当currentSatus为null的时候不能审核
	 */
	private static final long					serialVersionUID	= 1L;
	private ServiceSupplier						supplier;										// 中介
	private Boolean								auditted;										// 需要和currentSatus组合判断
	private UndeterminedRoster					undeterminedRoster;								// 所属待定名单
	private Date								startDate;										// 列入待定名单起始日期
	private Date								endDate;										// 列入待定名单截至日期
	private List<Attachment>					attachments;									// 整改情况上传
	private RecordStatus						recordStatus		= RecordStatus.Initaling;	// 当前状态（null：初始状态；true
																								// 删除状态；false 保留状态）
	private RejectRecord						rejectRecord;									// 驳回
	private List<DeterminedRecordAuditHistory>	auditHistory;									// 审核历史记录
	private Date								newEndDate;										// 当提报员点击保留按钮时，需要填写一个日期，交由审核审核，审核通过后，
	private User								auditUser;										// 审核人员
	private Date								auditDate;										// 审核日期
	private User								submitUser;										// 提交人员
	private Date								submitDate;										// 提交日期
	private boolean								hasTipReporter		= false;					// 已经给提报员提醒

	/*
	 * 2019-11-12
	 * 司冰
	 * 增加自动退出标志字段
	 */
	private Date								limitDate;										// 有效期至
	private Integer								limitDays;										// 有效天数

	@NotNull
	@OneToOne
	public ServiceSupplier getSupplier() {
		return supplier;
	}

	public void setSupplier( ServiceSupplier supplier ) {
		this.supplier = supplier;
	}

	public Boolean getAuditted() {
		return auditted;
	}

	public void setAuditted( Boolean auditted ) {
		this.auditted = auditted;
	}

	@Enumerated( EnumType.STRING )
	public RecordStatus getRecordStatus() {
		return recordStatus;
	}

	public void setRecordStatus( RecordStatus recordStatus ) {
		this.recordStatus = recordStatus;
	}

	@OneToOne( cascade = CascadeType.ALL )
	public RejectRecord getRejectRecord() {
		return rejectRecord;
	}

	public void setRejectRecord( RejectRecord rejectRecord ) {
		this.rejectRecord = rejectRecord;
	}

	@NotNull
	@ManyToOne
	public UndeterminedRoster getUndeterminedRoster() {
		return undeterminedRoster;
	}

	public void setUndeterminedRoster( UndeterminedRoster undeterminedRoster ) {
		this.undeterminedRoster = undeterminedRoster;
	}

	@OneToMany( cascade = { CascadeType.PERSIST, CascadeType.MERGE } )
	@JoinTable( name = "DETERMINEDRECORD_ATTACHMENT" )
	public List<Attachment> getAttachments() {
		return attachments;
	}

	public void setAttachments( List<Attachment> attachments ) {
		this.attachments = attachments;
	}

	@OneToMany( cascade = CascadeType.ALL )
	@JoinTable( name = "DETERMINEDRECORD_AUDITHISTORY" )
	public List<DeterminedRecordAuditHistory> getAuditHistory() {
		return auditHistory;
	}

	public void setAuditHistory( List<DeterminedRecordAuditHistory> auditHistory ) {
		this.auditHistory = auditHistory;
	}

	@Temporal( TemporalType.TIMESTAMP )
	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate( Date startDate ) {
		this.startDate = startDate;
	}

	@Temporal( TemporalType.TIMESTAMP )
	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate( Date endDate ) {
		this.endDate = endDate;
	}

	@Temporal( TemporalType.TIMESTAMP )
	public Date getNewEndDate() {
		return newEndDate;
	}

	public void setNewEndDate( Date newEndDate ) {
		this.newEndDate = newEndDate;
	}

	@OneToOne
	public User getAuditUser() {
		return auditUser;
	}

	public void setAuditUser( User auditUser ) {
		this.auditUser = auditUser;
	}

	@Temporal( TemporalType.TIMESTAMP )
	public Date getAuditDate() {
		return auditDate;
	}

	public void setAuditDate( Date auditDate ) {
		this.auditDate = auditDate;
	}

	@OneToOne
	public User getSubmitUser() {
		return submitUser;
	}

	public void setSubmitUser( User submitUser ) {
		this.submitUser = submitUser;
	}

	@Temporal( TemporalType.TIMESTAMP )
	public Date getSubmitDate() {
		return submitDate;
	}

	public void setSubmitDate( Date submitDate ) {
		this.submitDate = submitDate;
	}

	public boolean isHasTipReporter() {
		return hasTipReporter;
	}

	public void setHasTipReporter( boolean hasTipReporter ) {
		this.hasTipReporter = hasTipReporter;
	}

	@Temporal( TemporalType.DATE )
	public Date getLimitDate() {
		return limitDate;
	}

	public void setLimitDate( Date limitDate ) {
		this.limitDate = limitDate;
	}

	@Min(1L)
	public Integer getLimitDays() {
		return limitDays;
	}

	public void setLimitDays( Integer limitDays ) {
		this.limitDays = limitDays;
	}

	@Transient
	public boolean canEdit() {
		Calendar endCal = Calendar.getInstance();
		endCal.setTime( getEndDate() );
		// 截止日期的前五天之后才可编辑
		endCal.set( Calendar.DAY_OF_MONTH, endCal.get( Calendar.DAY_OF_MONTH ) - 5 );
		Date minus5DaysDate = endCal.getTime();
		Date now = new Date();
		if ( now.before( minus5DaysDate ) )
			return false;
		if ( getAuditted() != null && getAuditted() )
			return false;
		else
			return true;
	}

	@Transient
	public boolean canAudit() {
		if ( getRecordStatus().equals( RecordStatus.Initaling ) )
			return false;
		if ( getAuditted() != null )
			return false;

		return true;
	}

	@Transient
	public String getAudittedStr() {
		if ( getAuditted() == null ) {
			return "待审核";
		} else if ( getAuditted() ) {
			return "审核通过";
		} else {
			return "驳回";
		}
	}

	@Transient
	public String operationStr() {
		if ( getRecordStatus().equals( RecordStatus.Initaling ) )
			return "初始状态";
		else if ( getRecordStatus().equals( RecordStatus.Deleted ) )
			return "删除";
		else
			return "保留";
	}
}
